﻿using Microsoft.AspNetCore.Mvc;
using PlatformNetCore.Common;
using PlatformNetCore.Common.Helper;
using PlatformNetCore.Entity;
using PlatformNetCore.IService;
using PlatformNetCore.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace PlatformNetCore.Web.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class JobsController : BaseController
    {
        private readonly IJobsService jobsService;
        private readonly IBasisService basisService;
        public JobsController(IJobsService _jobsService, IBasisService _basisService)
        {
            jobsService = _jobsService;
            basisService = _basisService;
        }

        #region 招聘管理
        [RoleAuthorize("JobsList")]
        public IActionResult JobsList(string search = "",string jobID = "",string positionID="", int pageindex = 1, int pagesize = 15)
        {
            try
            {
                if (Request.IsAjaxRequest())
                {
                    Expression<Func<Jobs, bool>> predicate = s => s.IsDel == 0;
                    if (!string.IsNullOrWhiteSpace(search))
                    {
                        predicate = predicate.And(k => k.Title.Contains(search));
                    }
                    if (!string.IsNullOrWhiteSpace(positionID))
                    {
                        predicate = predicate.And(k => k.PositionID == positionID);
                    }
                    if (!string.IsNullOrWhiteSpace(jobID))
                    {
                        predicate = predicate.And(k => k.JobID == jobID);
                    }
                    return Json(jobsService.GetJobsPagedList(pageindex, pagesize, predicate));
                }
                ViewBag.PositionTypeDLL = basisService.GetCategoryListDLL(CategoryType.PositionType.ToString(), false, true);
                ViewBag.JobsTypeDLL = basisService.GetCategoryListDLL(CategoryType.JobsType.ToString(), false, true);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("JobsAdd")]
        public IActionResult JobsAdd()
        {
            try
            {
                Jobs model = new Jobs();
                ViewBag.PositionTypeDLL = basisService.GetCategoryListDLL(CategoryType.PositionType.ToString());
                ViewBag.JobsTypeDLL = basisService.GetCategoryListDLL(CategoryType.JobsType.ToString());
                return View(model);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("JobsAdd")]
        public IActionResult JobsAdd(Jobs model)
        {
            try
            {
                model.AddBy = CurrentUser.Login;
                return Json(jobsService.AddJobs(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 拷贝
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("JobsCopy")]
        public IActionResult JobsCopy(string id)
        {
            try
            {
                return Json(jobsService.CopyJobs(id, CurrentUser.Login));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("JobsDel")]
        public IActionResult JobsDel(string id)
        {
            try
            {
                List<string> ids = new List<string>() { id };
                return Json(jobsService.UpdateJobsState(ids, CurrentUser.Login, DataStatus.Deleted));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("JobsEdit")]
        public IActionResult JobsEdit(string id)
        {
            try
            {
                ResultResponse<Jobs> result = jobsService.GetJobs(id);
                ViewBag.PositionTypeDLL = basisService.GetCategoryListDLL(CategoryType.PositionType.ToString());
                ViewBag.JobsTypeDLL = basisService.GetCategoryListDLL(CategoryType.JobsType.ToString());
                return View(result.data);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("JobsEdit")]
        public IActionResult JobsEdit(Jobs model)
        {
            try
            {
                model.UpdateBy = CurrentUser.Login;
                return Json(jobsService.UpdateJobs(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        #endregion

    }
}